﻿using System;  
using System.Collections.Generic;
using System.Data;

using SiteUnity.Frame.Common.Tools.Class;
using SiteUnity.Layers.Entity.General;
using SiteUnity.Layers.Entity.Users; 

namespace SiteUnity.Layers.Core.Provider.Users
{
    public class UserProvider:Provider
    {
        public UserProvider()
        {
            SetDefaultConnection();
        }

        public List<User> selectUserByPage(Page page)
        {
            Database.clear();
            Database.addParameter("@page_guid", page.Guid);

            DataTable datatable = Database.executeQuery("SU_selectUserByPage");
            Database.close();

            return ClassMapper.ToClassList<User>(datatable);
        }

        public List<UserPageItem> selectUserPageItemByPage(Page page)
        {
            Database.clear();
            Database.addParameter("@page_guid", page.Guid);

            DataTable datatable = Database.executeQuery("SU_selectUserPageItemByPage");
            Database.close();

            return ClassMapper.ToClassList<UserPageItem>(datatable);
        }

        public User insertUser(User user, Website website)
        {
            Database.clear();
            Database.addParameter("@name", user.Name);
            Database.addParameter("@surname", user.Surname);
            Database.addParameter("@email", user.Email);
            Database.addParameter("@website", user.Website);
            Database.addParameter("@websiteGuid", website.Guid);

            DataTable datatable = Database.executeQuery("SU_insertUser");
            Database.close();

            if (datatable.Rows.Count != 0)
                return ClassMapper.ToClass<User>(datatable.Rows[0]);
            else
                return user;
        }

        public void insertUserPageItem(UserPageItem userPageItem)
        {
            Database.clear();
            Database.addParameter("@userGuid", userPageItem.userGuid);
            Database.addParameter("@pageItemGuid", userPageItem.pageItemGuid);

            Database.executeNonQuery("SU_insertUserPageItem");
            Database.close();
        }
    }
}
